﻿using System;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Messaging;
using MicroMvvm;

namespace Example.ViewModel
{
    public class EventToCommandViewModel : ObservableObject
    {

        public EventToCommandViewModel()
        {
            Messenger.Default.Register<string>(this, EventPanelViewModel.AlertMessageToken, ShowReceiveInfo);
        }

        private string _tipText;
        private bool _isReceiveMouseMove = true;
        private RelayCommand _loadedCommand;
        private RelayCommand<MouseEventArgs> _mouseMoveCommand;

        private void ShowReceiveInfo(string msg)
        {
            TipText = $"我也收到EventPanelViewModel传来的消息了：{msg}";
        }

        public bool IsReceiveMouseMove
        {
            get => _isReceiveMouseMove;
            set
            {
                _isReceiveMouseMove = value;
                OnPropertyChanged();
            }
        }

        public string TipText
        {
            get => _tipText;
            set
            {
                _tipText = value;
                OnPropertyChanged();
            }
        }
     
        public RelayCommand LoadedCommand
        {
            get
            {
                if (_loadedCommand == null)
                {
                    _loadedCommand = new RelayCommand(OnLoaded);
                }
                return _loadedCommand;
            }
        }
      
        public RelayCommand<MouseEventArgs> MouseMoveCommand
        {
            get
            {
                if (_mouseMoveCommand == null)
                {
                    _mouseMoveCommand = new RelayCommand<MouseEventArgs>(OnMouseMove, o => IsReceiveMouseMove);
                }
                return _mouseMoveCommand;
            }
        }
       
        public void OnMouseDown(object sender, MouseEventArgs e)
        {
            var point = e.GetPosition(e.Device.Target);
            TipText = $"鼠标点击 位置  X:{point.X}  Y:{point.Y}";
        }


        private void OnLoaded()
        {
            //MessageBox.Show("程序加载完毕！");
        }

        private void OnMouseMove(MouseEventArgs e)
        {
            var point = e.GetPosition(e.Device.Target);
            var left = "左键放开";
            var mid = "中键放开";
            var right = "右键放开";

            if (e.LeftButton == MouseButtonState.Pressed)
                left = "左键按下";
            if (e.MiddleButton == MouseButtonState.Pressed)
                mid = "中键按下";
            if (e.RightButton == MouseButtonState.Pressed)
                right = "右键按下";

            TipText = $"当前鼠标位置  X:{point.X}  Y:{point.Y}  当前鼠标状态:{left} {mid}  {right}";
        }

    }
}
